﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace teach
{
    public class Question
    {
        private Dictionary<string, string> m_Properties = new Dictionary<string, string>();
        public Dictionary<string, string> Properties
        {
            get
            {
                return m_Properties;
            }
            set
            {
                m_Properties = value;
            }
        }

        public QuestionType Type { get; set; }

        public int Timeout { get; set; } 
        public int First { get; set; }
        public int Second { get; set; }
        public int NumericAnswer
        {
            get
            {
                int result = 0;
                switch (Type.Name)
                {
                    case "Addition":
                        result = First + Second;
                        break;
                    case "Substraction": 
                        result = First - Second;
                        break;
                    case "Multiplication": 
                        result = First * Second;
                        break;
                    case "Division": 
                        result = First / Second;
                        break;
                }
                return result;
            }
        }

        public void Log(String line)
        {
        //    StreamWriter writer = new StreamWriter("C:\\proflog.txt", true);
        //    writer.WriteLine(line);
        //    writer.Close();
        }

        public bool CheckAnswer(int num, string text)
        {
            bool result = false;
            switch (Type.Name)
            {
                case "Addition":
                    result = (num == NumericAnswer);
                    break;
                case "Substraction":
                    result = (num == NumericAnswer);
                    break;
                case "Multiplication":
                    result = (num == NumericAnswer);
                    break;
                case "Division":
                    result = (num == NumericAnswer);
                    break;
                case "English":
                    result = (text == Properties["english"]);
                    break;
                case "Mots":
                    result = (text == Properties["word"]);
                    break;
                case "Unités":
                    result = (text == this.Second.ToString());
                    break;
                case "Math":
                    result = (text == this.Properties["answer"]);
                    break;
                case "Choix":
                    result = (text.ToLower() == this.Properties["Good"].ToLower());
                    break;
                case "Histoire":
                    result = (text.ToLower() == Properties["answer"].ToLower());
                    break;
                case "Conjug":
                    result = (text.ToLower() == Properties["answer"].ToLower());
                    break;
            }
            return result;
        }

        public bool isNumeric()
        {
            return (Type.Name == "Addition" || Type.Name == "Substraction"
                || Type.Name == "Multiplication" || Type.Name == "Division");
        }

        public string GetSign()
        {
            string text = "";
            switch (Type.Name)
            {
                case "Addition":
                    text = "+";
                    break;
                case "Substraction":
                    text = "-";
                    break;
                case "Multiplication":
                    text = "X";
                    break;
                case "Division":
                    text = "÷";
                    break;
            }
            return text;
        }

        public string[] GetChoices()
        {
            string[] choices = { Properties["answerA"], Properties["answerB"], Properties["answerC"] };
            return choices;
        }

        public string GetAnswer()
        {
            string text = "";
            switch (Type.Name)
            {
                case "Addition":
                    text = NumericAnswer.ToString();
                    break;
                case "Substraction":
                    text = NumericAnswer.ToString();
                    break;
                case "Multiplication":
                    text = NumericAnswer.ToString();
                    break;
                case "Division":
                    text = NumericAnswer.ToString();
                    break;
                case "English":
                    text = Properties["english"];
                    break;
                case "Mots":
                    text = Properties["word"];
                    break;
                case "Unités":
                    text = this.Second.ToString();
                    break;
                case "Math":
                    text = this.Properties["answer"];
                    break;
                case "Histoire":
                    text = this.Properties["answer"];
                    break;
                case "Conjug":
                    text = this.Properties["answer"];
                    break;
                case "Choix":
                    text = this.Properties["Good"];
                    break;
            }
            return text;
        }

        public string GetTextQuestion()
        {
            string text = "";
            switch (Type.Name)
            {
                case "English":
                    text = Properties["french"];
                    break;
                case "Mots":
                    text = Properties["image"];
                    break;
                case "Unités":
                    text = Properties["question"];
                    text = text.Replace("%1", this.First.ToString());
                    break;
                case "Math":
                    text = Properties["question"];
                    text = text.Replace("%1", this.Second.ToString());
                    text = text.Replace("%2", this.First.ToString());
                    break;
                case "Histoire":
                    text = Properties["question"];
                    break;
                case "Conjug":
                    text = Properties["question"];
                    break;
                case "Choix":
                    text = Properties["question"];
                    break;
            }
            return text;
        }


        /// <summary>
        /// Generate the question
        /// </summary>
        public void GenerateQuestion(int option)
        {
            Random rnd = new Random();
            switch (Type.Name)
            {
                case "Addition":
                    //First = rnd.Next(Type.Settings[option].Max - Type.Settings[option].Min) + Type.Settings[option].Min;
                    //Second = rnd.Next(Type.Settings[option].Max - Type.Settings[option].Min) + Type.Settings[option].Min;
                    break;
                case "Substraction":
                    //First = rnd.Next(Type.Settings[option].Max - Type.Settings[option].Min) + Type.Settings[option].Min;
                    //Second = rnd.Next(Type.Settings[option].Max - Type.Settings[option].Min) + Type.Settings[option].Min;
                    //while (First < Second || First == Second)
                    //{
                    //    First = rnd.Next(Type.Settings[option].Max - Type.Settings[option].Min) + Type.Settings[option].Min;
                    //    Second = rnd.Next(Type.Settings[option].Max - Type.Settings[option].Min) + Type.Settings[option].Min;
                    //}
                    break;
                case "Multiplication":
                    //First = rnd.Next(Type.Settings[option].Max - Type.Settings[option].Min) + Type.Settings[option].Min;
                    //Second = rnd.Next(Type.Settings[option].Max - Type.Settings[option].Min) + Type.Settings[option].Min;
                    break;
                case "Division":
                    //First = rnd.Next(Type.Settings[option].Max - Type.Settings[option].Min) + Type.Settings[option].Min;
                    //Second = rnd.Next(Type.Settings[option].Max - Type.Settings[option].Min) + Type.Settings[option].Min;
                    //while (First < Second || (First % Second != 0))
                    //{
                    //    First = rnd.Next(Type.Settings[option].Max - Type.Settings[option].Min) + Type.Settings[option].Min;
                    //    Second = rnd.Next(Type.Settings[option].Max - Type.Settings[option].Min) + Type.Settings[option].Min;
                    //}
                    break;
                case "English":
                    Question newQuestion = Type.TextQuestions[rnd.Next(Type.TextQuestions.Count)];
                    while (option == 1 && newQuestion.Properties["kids"] == "Philippe")
                    {
                        newQuestion = Type.TextQuestions[rnd.Next(Type.TextQuestions.Count)];
                    }
                    this.Properties = newQuestion.Properties;
                    this.Timeout = newQuestion.Timeout;
                    break;
                case "Mots":
                    Question wordQuestion = Type.TextQuestions[rnd.Next(Type.TextQuestions.Count)];
                    while ((option == 1 && wordQuestion.Properties["kids"] == "philippe")
                        || (option == 0 && wordQuestion.Properties["kids"] == "simon"))
                    {
                        wordQuestion = Type.TextQuestions[rnd.Next(Type.TextQuestions.Count)];
                    }
                    this.Properties = wordQuestion.Properties;
                    this.Timeout = wordQuestion.Timeout;
                    break;
                case "Unités":
                    Question unitQuestion = Type.TextQuestions[rnd.Next(Type.TextQuestions.Count)];
                    this.Properties = unitQuestion.Properties;
                    this.Timeout = unitQuestion.Timeout;
                    int divider = int.Parse(Properties["divide"]);
                    if (divider == 100)
                    {
                        this.First = ((rnd.Next(9) + 1) * 100) + ((rnd.Next(9) + 1) * 10) + rnd.Next(10);
                        this.Second = (int)(Math.Floor(((float)this.First) / 100F));
                    }
                    else if (divider == 10)
                    {
                        this.First = ((rnd.Next(9)) * 100) + ((rnd.Next(9) + 1) * 10) + rnd.Next(10);
                        this.Second = (int)(Math.Floor(((float)this.First) / 10F));
                    }
                    else
                    {
                        this.First = rnd.Next(999);
                        this.Second = this.First;
                    }
                    break;
                case "Histoire":
                    Question histQuestion = Type.TextQuestions[rnd.Next(Type.TextQuestions.Count)];
                    while (option == 1 && histQuestion.Properties["kids"] == "Philippe")
                    {
                        histQuestion = Type.TextQuestions[rnd.Next(Type.TextQuestions.Count)];
                    }
                    this.Properties = histQuestion.Properties;
                    this.Timeout = histQuestion.Timeout;
                    break;
                case "Conjug":
                    Question conjugQuestion = Type.TextQuestions[rnd.Next(Type.TextQuestions.Count)];
                    while (option == 1 && conjugQuestion.Properties["kids"] == "Philippe")
                    {
                        conjugQuestion = Type.TextQuestions[rnd.Next(Type.TextQuestions.Count)];
                    }
                    this.Properties = conjugQuestion.Properties;
                    this.Timeout = conjugQuestion.Timeout;
                    break;
                case "Choix":
                    Question choiceQuestion = Type.TextQuestions[rnd.Next(Type.TextQuestions.Count)];
                    while (option == 1 && choiceQuestion.Properties["kids"] == "Philippe"
                        || option == 0 && choiceQuestion.Properties["kids"] == "Simon")
                    {
                        choiceQuestion = Type.TextQuestions[rnd.Next(Type.TextQuestions.Count)];
                    }
                    this.Properties = choiceQuestion.Properties;
                    this.Timeout = choiceQuestion.Timeout;
                    break;
                case "Math":
                    Question mathQuestion = Type.TextQuestions[rnd.Next(Type.TextQuestions.Count)];
                    Log("mathQuestion.Properties[max]: " + mathQuestion.Properties["max"]);
                    Log("mathQuestion.Properties[divide]: " + mathQuestion.Properties["divide"]);
                    this.Properties = mathQuestion.Properties;
                    this.Timeout = mathQuestion.Timeout;
                    int max = int.Parse(Properties["max"]);
                    if (option == 1)
                    {
                        max = int.Parse(Properties["maxSimon"]);
                    }
                    Log("this.Properties[max]: " + this.Properties["max"]);
                    Log("this.Properties[divide]: " + this.Properties["divide"]);
                    
                    if (Properties["divide"] == "multiple")
                    {
                        this.Second = rnd.Next(max);
                        this.First = rnd.Next(max);
                        while (this.Second == 0 || this.First == 0)
                        {
                            this.Second = rnd.Next(max);
                            this.First = rnd.Next(max);
                        }
                        this.Properties["answer"] = this.First.ToString();
                        this.First = this.Second * this.First;

                    }
                    else if (Properties["divide"] == "plus")
                    {
                        this.First = rnd.Next(max);
                        this.Second = rnd.Next(this.First);
                        this.Properties["answer"] = (this.First - this.Second).ToString();
                    }
                    else if (Properties["divide"] == "minusA")
                    {
                        this.Second = rnd.Next(max);
                        this.First = rnd.Next(this.Second);
                        this.Properties["answer"] = (this.Second - this.First).ToString();
                    }
                    else if (Properties["divide"] == "minusB")
                    {
                        this.First = rnd.Next(max);
                        this.Second = rnd.Next(this.First);
                        this.Properties["answer"] = (this.First + this.Second).ToString();
                    }
                    Log("this.First: " + this.First);
                    Log("this.Second: " + this.Second);
                    Log("this.Properties[answer]: " + this.Properties["answer"]);
                    break;
            }
        }
    }
}
